﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

namespace Pap.Data
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class ValidatorBase
    {
        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, string> _attributes;
        /// <summary>
        /// Initializes a new instance of the <see cref="ValidatorBase"/> class.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public ValidatorBase(Dictionary<string, string> attributes)
        {
            _attributes = attributes;
        }
        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified key.
        /// </summary>
        public string this[string key]
        {
            get
            {
                return this._attributes[key];
            }
        }

        /// <summary>
        /// Validates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public virtual bool Validate(object obj)
        {
            if (obj == null)
                return true;
            if (string.IsNullOrEmpty(obj.ToString()))
                return true;
            return false;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class RequiredValidator : ValidatorBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RequiredValidator"/> class.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public RequiredValidator(Dictionary<string, string> attributes)
            : base(attributes)
        { }

        /// <summary>
        /// Validates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            return !base.Validate(obj);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class RegularExpresionValidator : ValidatorBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RegularExpresionValidator"/> class.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public RegularExpresionValidator(Dictionary<string, string> attributes)
            : base(attributes) { }

        /// <summary>
        /// Gets the reqular expression.
        /// </summary>
        public string ReqularExpression
        {
            get
            {
                return this["reqex"];
            }
        }

        /// <summary>
        /// Validates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            if (base.Validate(obj))
                return true;
            return Regex.IsMatch(obj.ToString(), ReqularExpression);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class DataTypeValidator : ValidatorBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataTypeValidator"/> class.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public DataTypeValidator(Dictionary<string, string> attributes)
            : base(attributes) { }


        /// <summary>
        /// Gets the type of the data.
        /// </summary>
        /// <value>
        /// The type of the data.
        /// </value>
        public ValidationDataType DataType
        {
            get
            {
                return (ValidationDataType)Enum.Parse(typeof(ValidationDataType), this["DataType"]);
            }
        }

        /// <summary>
        /// Validates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            if (base.Validate(obj))
                return true;
            return CompareValidator.CanConvert(obj.ToString(), DataType);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class StringValidator : ValidatorBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="StringValidator"/> class.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public StringValidator(Dictionary<string, string> attributes)
            : base(attributes) { }

        /// <summary>
        /// Gets the length of the min.
        /// </summary>
        /// <value>
        /// The length of the min.
        /// </value>
        public int MinLength
        {
            get
            {
                return int.Parse(this["MinLength"]);
            }
        }
        /// <summary>
        /// Gets the length of the max.
        /// </summary>
        /// <value>
        /// The length of the max.
        /// </value>
        public int MaxLength
        {
            get
            {
                return int.Parse(this["MaxLength"]);
            }
        }
        /// <summary>
        /// Gets the invalid characters.
        /// </summary>
        public string InvalidCharacters
        {
            get
            {
                return this["InvalidCharacters"];
            }
        }

        /// <summary>
        /// Validates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            if (base.Validate(obj))
                return true;
            try
            {
                System.Configuration.StringValidator strvld = new System.Configuration.StringValidator(this.MinLength, this.MaxLength,
                    this.InvalidCharacters);
                strvld.Validate(obj);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class DateValidator : ValidatorBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DateValidator"/> class.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        public DateValidator(Dictionary<string, string> attributes)
            : base(attributes) { }

        /// <summary>
        /// Gets the date format.
        /// </summary>
        public string DateFormat
        {
            get
            {
                return this["DateFormat"];
            }
        }

        /// <summary>
        /// Validates the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            if (base.Validate(obj))
                return true;
            return false;
        }
    }

}
